package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;

import cn.edu.zjgsu.ito.model.Major;
import cn.edu.zjgsu.ito.service.AdminMajManageService;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

@Before(AdminControllerInterceptor.class)
public class AdminMajManageController extends Controller {

	AdminMajManageService adminMajManageService=ServiceFactory.createAdminMajManageService();

	/**
	 * 专业管理初始化
	 * 黄凯航
	 */
	public void index()
	{
		List<Record> majors=Db.find("select * from major");
		List<Record> colleges=Db.find("select collegeCode from major");

		HashSet<Record> c=new HashSet<Record>(colleges);
		colleges.clear();
		colleges.addAll(c);

		setAttr("college",colleges);
		setAttr("major",majors);			
		setAttr("collegeCode",Db.find("select * from college"));
		render("majormanage.html");
	}
	/**
	 * 添加专业页面跳转
	 * 黄凯航
	 */
	public void add()
	{
		render("addmajor.html");
	}
	/**
	 * 添加专业
	 * 黄凯航
	 */
	public void submit()
	{
		Major major = getModel(Major.class,"major");
		System.out.println(major);
		String error=adminMajManageService.addSingleMajor(major);
		setAttr("error",error);
		index();
	}
	/**
	 * 编辑专业页面跳转
	 * 黄凯航
	 */
	public void edit()
	{
		Integer id=getParaToInt(0);
		if(id!=null&&id>0)
			setAttr("major",Major.dao.findById(id));
		render("correctmajor.html");
	}
	/**
	 * 更新专业
	 * 黄凯航
	 */
	public void update()
	{
		Major major = getModel(Major.class,"major");
		String error=adminMajManageService.updateOrNot(major);
		setAttr("error",error);
		index();
	}
	/**
	 * 异步更新专业
	 * 黄凯航
	 */
	public void ajaxupdate()
	{
		String majorName=getPara("majorName");
		String collegeCode=getPara("collegeCode");
		String majorNo=getPara("majorNo");
		int id=getParaToInt("id");

		Major major = getModel(Major.class);
		major.set("majorName", majorName);
		major.set("collegeCode", collegeCode);
		major.set("id", id);
		major.set("majorNo", majorNo);

		String error=adminMajManageService.updateOrNot(major);
		setAttr("major",Major.dao.find("select * from major where id=?",id));
		setAttr("error",error);
		System.out.println(error);
		System.out.println(major);
		renderJson();
	}
	/**
	 * 专业搜索
	 * 黄凯航
	 */
	public void search()
	{	
		String keyword=getPara("keyword");
		if(keyword.equals("all"))
			index();
		else
		{
			List<Record> majors=Db.find("select * from major where collegeName=?",keyword);
			setAttr("major",majors);
			List<Record> colleges=Db.find("select collegeName from major");

			HashSet<Record> c=new HashSet<Record>(colleges);
			colleges.clear();
			colleges.addAll(c);

			setAttr("college",colleges);
			render("majormanage.html");
		}
	}
	/**
	 * 异步专业搜索
	 * 黄凯航
	 */
	public void ajaxsearch()
	{
		String keyword=getPara("keyword");

		if(keyword.equals("all"))
		{
			List<Major> majors=Major.dao.find("select * from major");
			renderJson(majors);
		}

		else
		{
			List<Major> majors=Major.dao.find("select * from major where collegeCode=?",keyword);
			renderJson(majors);
		}
	}


	/**
	 * 批量删除
	 * 黄凯航
	 */
	public void batch()
	{
		try{
			int id=0;
			//String[] majCheckboxs=getParaValues("majCheckbox");
			String checkboxValue=getPara("checkboxValue");
			String[] majCheckboxs=checkboxValue.split(",");
			for(int i=0;i<majCheckboxs.length;i++)
				System.out.print(i+"="+majCheckboxs[i]+"  ");
			Record majorMajorName=new Record();
			String majorNo="";
			for(int i=0;i<majCheckboxs.length;i++)
			{
				id=Integer.parseInt(majCheckboxs[i]);
				majorMajorName=Db.findById("major", id);
				majorNo=majorMajorName.get("majorNo");
				adminMajManageService.deleteOrNot(majorNo);
				majorMajorName=new Record();
			}
			redirect("/adminmajmanage");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redirect("/adminmajmanage");
		}
	}	
	/**
	 * 专业模板下载方法
	 * 黄凯航
	 */
	public void fileDownload()
	{
		String fileName="专业模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	/**
	 * 专业模板上传方法
	 * 黄凯航
	 */
	public void fileUpload()
	{
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				FileOutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				fos.close();
				fis.close();
				source.delete();
				String error=adminMajManageService.readMajorTemplate(fileName, newFileName, filedir);
				setAttr("error",error);
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
